import { mapcity } from '@/api/map'
import { get } from '@/utils/localStorage'
// 生成26字母空数组的对象
export const objdaxie = (a, b) => {
  //  'A'.charCodeAt()
  // String.fromCharCode(ascii码)
  // toUpperCase()
  const obj = {}
  for (let i = a; i <= b; i++) {
    obj[String.fromCharCode(i)] = []
  }
  return obj
}
// 生成26字母的数组
export const arrlist = (a, b) => {
  const arr = []
  for (let i = a; i <= b; i++) {
    arr.push(String.fromCharCode(i))
  }
  return arr
}
// 处理城市列表最终数据
export const chengshilist = (obj, arr) => {
  const blist = ['北京', '上海', '广州', '深圳']
  arr.forEach((item) => {
    if (blist.includes(item.label)) return
    let k
    for (k in obj) {
      if (k === item.short[0].toUpperCase()) {
        obj[k].push(item)
      }
    }
  })
  let k
  for (k in obj) {
    if (obj[k].length === 0) {
      obj[k].push({ label: '暂无城市' })
    }
  }
  return obj
}
// 处理数据 添加children
export const addchildren = (obj) => {
  let key
  for (key in obj) {
    if (obj[key] instanceof Array || obj[key] instanceof Object) {
      addchildren(obj[key])
    } else {
      if (!Object.keys(obj).includes('children')) {
        obj.children = ['']
      }
    }
  }
  return obj
}
// 处理数据 添加flag
export const addflag = (arr, f) => {
  arr.forEach((item) => (item.flag = f))
  return arr
}
// 处理区域数据并拿到对应value值
export const addvalue = (arr, index) => {
  if (index[2] === 0) {
    return arr[index[0]].children[index[1]].value
  } else {
    return arr[index[0]].children[index[1]].children[index[2]].value
  }
}

// 处理标签数据并拿到对应value值

export const labeldata = (arr) => {
  const value = arr.find((item) => item.flag === true)
  if (value) {
    return value.value
  }
}

// 地图

export const dt = async (obj, i) => {
  const { BMapGL } = window
  const map = new BMapGL.Map('container')
  const city = JSON.parse(get()).label + '市'
  map.centerAndZoom(city, i) // 初始化地图,设置中心点坐标和地图级别
  const res = await mapcity(obj)
  console.log(res)
  res.data.body.forEach((item) => {
    const point = new BMapGL.Point(item.coord.longitude, item.coord.latitude)
    const content = item.label + '<br>' + item.count
    const label = new BMapGL.Label(content, {
      // 创建文本标注
      position: point,
      offset: new BMapGL.Size(10, 20)
    })
    map.addOverlay(label) // 将标注添加到地图中
    label.setStyle({
      // 设置label的样式
      color: '#fff',
      fontSize: '16px',
      width: '60px',
      height: '60px',
      borderRadius: '50%',
      background: 'rgb(23, 160, 10)',
      textAlign: 'center',
      paddingTop: '10px'
    })
    label.addEventListener('click', function () {
      // alert('您点击了标注')
      map.clearOverlays()
      dt({ id: item.value }, i + 2)
    })
  })
}
